﻿using System;
using Dispatcher.ViewModels;
using Dispatcher.ViewModels.Base;
using Dispatcher.Views;
using Repository;

namespace Dispatcher.Helpers
{
    /// <summary>
    /// View Model Factory
    /// </summary>
    internal static class ViewModelFactory
    {
        /// <summary>
        /// Creates the model.
        /// </summary>
        /// <param name="viewModelType">Type of the view model.</param>
        /// <param name="constructorArgs">The constructor args.</param>
        /// <returns>
        /// View model instance.
        /// </returns>
        public static ViewModelBase CreateViewModel(Type viewModelType, params object[] constructorArgs)
        {
            var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType, constructorArgs);
            viewModel.CommandExecuted += MainWindow.CommandExecuted;
            return viewModel;
        }

        /// <summary>
        /// Creates the object set view model.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="modelContainer">The model container.</param>
        /// <returns>
        /// View model instance.
        /// </returns>
        public static ViewModelBase CreateObjectSetViewModel(Type entityType, ModelContainer modelContainer)
        {
            var viewModelType = typeof(EntityListViewModel<>).MakeGenericType(entityType);
            var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType, modelContainer);
            viewModel.CommandExecuted += MainWindow.CommandExecuted;
            return viewModel;
        }

        /// <summary>
        /// Creates the object set view model.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="entityCollection">The entity collection.</param>
        /// <returns>
        /// View model instance.
        /// </returns>
        public static ViewModelBase CreateEntityCollectionViewModel(Type entityType, object entityCollection)
        {
            var viewModelType = typeof(EntityListViewModel<>).MakeGenericType(entityType);
            var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType, entityCollection);
            viewModel.CommandExecuted += MainWindow.CommandExecuted;
            return viewModel;
        }

        /// <summary>
        /// Creates the view model.
        /// </summary>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="genricArg">The genric arg.</param>
        /// <param name="modelContainer">The model container.</param>
        /// <returns>
        /// View model instance.
        /// </returns>
        public static ViewModelBase CreateViewModel<TViewModel>(Type genricArg, ModelContainer modelContainer)
        {
            var viewModelType = typeof(TViewModel).MakeGenericType(genricArg);
            var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType, modelContainer);
            viewModel.CommandExecuted += MainWindow.CommandExecuted;
            return viewModel;
        }

        /// <summary>
        /// Creates the view model.
        /// </summary>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="genricArgs">The genric args.</param>
        /// <param name="modelContainer">The model container.</param>
        /// <returns>
        /// View model instance.
        /// </returns>
        public static ViewModelBase CreateViewModel<TViewModel>(Type[] genricArgs, ModelContainer modelContainer)
        {
            var viewModelType = typeof(TViewModel).MakeGenericType(genricArgs);
            var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType, modelContainer);
            viewModel.CommandExecuted += MainWindow.CommandExecuted;
            return viewModel;
        }
    }
}
